package javalab.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

/*
项目：watermark4J v0.2.2
创建人：曾逸夫
创建时间：2023年8月16日
功能描述：创建水印
 */
public class Watermark {
    // 文字水印
    public static void addWatermark(String imgPath, String imgWMSavePath, String watermarkContent, String WMLoadMode, String WMDistMode, int degree) throws IOException {
        // --------- 水印属性 ---------
        String fontName = "微软雅黑";
        int fontStyle = Font.BOLD;
        int fontSize = 20;
//        int[] fontColor = {255, 0, 0, 128};

        // ---------- 图片加载模式 ----------
        Image img = null;
        if (WMLoadMode.equals("local")){
            img = ImageIO.read(new File(imgPath));
        } else {
            img = ImageIO.read(new URL(WMLoadMode).openStream());
        }

        int w = img.getWidth(null);  // 原图的宽度
        int h = img.getHeight(null);  // 原图的高度

        // -------------- 加水印 --------------
        BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.drawImage(img, 0, 0, w, h, null);
//        graphics2D.setColor(new Color(fontColor[0], fontColor[1], fontColor[2], fontColor[3]));
        graphics2D.setColor(new Color(127, 164, 210, 96));  // 便于IDEA调节颜色
        graphics2D.setFont(new Font(fontName, fontStyle, fontSize));
        graphics2D.rotate(Math.toRadians(degree));

        //设置水印的坐标
        int wm_width = graphics2D.getFontMetrics(graphics2D.getFont()).charsWidth(watermarkContent.toCharArray(), 0, watermarkContent.length());
        int wm_xc = (w - wm_width) / 2;
        int wm_yc = h / 2;

        if (WMDistMode.equals("single")){
            graphics2D.drawString(watermarkContent, wm_xc, wm_yc);
        } else if (WMDistMode.equals("tiling")) {
            int rows = w / wm_width;
            int cols = h / wm_width;
            if(rows < 1){
                rows = 1;
            }
            if(cols < 1){
                cols = 1;
            }
            for(int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {
                    graphics2D.drawString(watermarkContent, row * wm_width + col * wm_width, -row * wm_width + col * wm_width);
                }
            }
        }else {
            System.err.println("水印样式出现问题！");
            return;
        }
        graphics2D.dispose();

        // 输出图片
        FileOutputStream outImgStream = new FileOutputStream(imgWMSavePath);
        ImageIO.write(bufferedImage, "png", outImgStream);
        System.out.println("水印添加完成！");
        outImgStream.flush();
        outImgStream.close();
    }


    // 图片水印
    public static void addImgTypeWatermark(String imgPath, String imgWMSavePath, String watermarkImgContent, String WMLoadMode, String WMDistMode, int degree) throws IOException {
        // ---------- 图片加载模式 ----------
        Image img = null;
        if (WMLoadMode.equals("local")){
            img = ImageIO.read(new File(imgPath));
        } else {
            img = ImageIO.read(new URL(WMLoadMode).openStream());
        }

        int w = img.getWidth(null);  // 原图的宽度
        int h = img.getHeight(null);  // 原图的高度

        // -------------- 加水印 --------------
        BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.drawImage(img, 0, 0, w, h, null);
        graphics2D.rotate(Math.toRadians(degree));

        Image WMImg = ImageIO.read(new File(watermarkImgContent));
        int wm_width = WMImg.getWidth(null);
        int wm_height = WMImg.getHeight(null);

        graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
        if (WMDistMode.equals("single")){
            graphics2D.drawImage(WMImg, (w - wm_width) / 2, (h - wm_height) / 2, wm_width, wm_height, null);
        } else if (WMDistMode.equals("tiling")) {
            int rows = w / wm_width;
            int cols = h / wm_width;
            if(rows < 1){
                rows = 1;
            }
            if(cols < 1){
                cols = 1;
            }
            for(int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {
                    graphics2D.drawImage(WMImg, row * wm_width + col * wm_width, -row * wm_width + col * wm_width, wm_width, wm_height, null);
                }
            }
        }else {
            System.err.println("水印样式出现问题!");
            return;
        }
        graphics2D.dispose();

        // 输出图片
        FileOutputStream outImgStream = new FileOutputStream(imgWMSavePath);
        ImageIO.write(bufferedImage, "png", outImgStream);
        System.out.println("水印添加完成!");
        outImgStream.flush();
        outImgStream.close();
    }


    // 批量水印
    public static void addBatchWatermark(String batchMode, String imgPath, String imgWMSavePath, String watermarkContent, String WMLoadMode, String WMDistMode, int degree) throws IOException {
        File file = new File(imgPath);
        File[] tempList = file.listFiles();
        int img_index = 0;

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                String fileNamePath = String.valueOf(tempList[i]);
                if (!checkFile(fileNamePath)){
                    continue;
                }
                img_index ++;
                String[] fileNamePathArray = fileNamePath.split("\\\\");
                String fileName = fileNamePathArray[fileNamePathArray.length-1];
                String imgWMSavePathNew = imgWMSavePath + "\\text_" +fileName;

                if (batchMode.equals("text")){
                    System.out.println(img_index + ": " + fileName + " 正在添加水印。。。");
                    addWatermark(fileNamePath, imgWMSavePathNew, watermarkContent, WMLoadMode, WMDistMode, degree);
                } else if (batchMode.equals("img")) {
                    System.out.println(fileName + " 正在添加水印。。。");
                    addImgTypeWatermark(fileNamePath, imgWMSavePathNew, watermarkContent, WMLoadMode, WMDistMode, degree);
                }else {
                    System.err.println("模式错误！");
                    return;
                }
            }
        }
    }

    // 图片格式检查
    public static boolean checkFile(String fileName){
        if (fileName.endsWith("jpg") || fileName.endsWith("jpeg") || fileName.endsWith("png") || fileName.endsWith("tif")){
            return true;
        }else {
            return false;
        }
    }

}
